package com.waitingresult.system.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.waitingresult.common.base.model.Result;
import com.waitingresult.common.constant.Constants;
import com.waitingresult.common.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * All right reserved,Designed By WangShuai
 * <p>
 * 短信发送接口
 *
 * @author : WangShuai
 * @version : V1.0
 * @ClassName : SmsController
 * @Description :
 * @date : 2021/9/29 16:48
 */
@RestController
@RequestMapping(value = "sms")
@Api(tags = "短信发送")
public class SmsController {

    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
    @PostMapping("send")
    public Result<Object> send(String phone) {
        //生成验证码
        String codeStr = RandomUtil.randomNumbers(Constants.SmsSuffix.NUMBER_STRING_LENTH);

        //发送短信

        // 等 短信发送成功 在存入Redis
        RedisUtils.StringOps.set(Constants.SmsSuffix.STRING_SUFFIX + phone, codeStr);
        return Result.success("ok");
    }

    @ApiOperation(value = "校验验证码", notes = "校验验证码")
    @PostMapping("check")
    public Result<Object> check(String phone, String code) {
        String codeStr = RedisUtils.StringOps.get(Constants.SmsSuffix.STRING_SUFFIX + phone);
        if (StrUtil.isNotEmpty(codeStr) && StrUtil.equals(codeStr, code)) {
            return Result.success("校验通过");
        }
        return Result.error("校验失败");
    }
}
